<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="java,后端,程序员">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="Axuan">
    
    <title>
        
            JS代码片段 |
        
        阿轩的代码世界
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"blog.ctdx.store","root":"/","language":"zh-CN"};
    KEEP.theme_config = {"toc":{"enable":true,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#0066CC","avatar":"/images/avatar.svg","favicon":"/images/logo.svg","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"/images/bg2.jpg","description":null},"scroll":{"progress_bar":{"enable":true},"percent":{"enable":false}}},"local_search":{"enable":true,"preload":true},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":true},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 6.0.0"><link rel="alternate" href="/atom.xml" title="阿轩的代码世界" type="application/atom+xml">
</head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                阿轩的代码世界
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/tags"
                            >
                                标签
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/links"
                            >
                                友链
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/tags">标签</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/links">友链</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">JS代码片段</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="/images/avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">Axuan</span>
                        
                            <span class="author-label">Lv1</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2022-03-09 13:33:04</span>
        <span class="mobile">2022-03-09 13:33</span>
    </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/%E5%89%8D%E7%AB%AF/">前端</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>3.9k 字</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>21 分钟</span>
        </span>
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <ol>
<li>邮箱<br>export const isEmail &#x3D; (s) &#x3D;&gt; {    return &#x2F;^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$&#x2F;.test(s)}</li>
<li>手机号码<br>export const isMobile &#x3D; (s) &#x3D;&gt; {    return &#x2F;^1[0-9]{10}$&#x2F;.test(s)}</li>
<li>电话号码<br>export const isPhone &#x3D; (s) &#x3D;&gt; {    return &#x2F;^([0-9]{3,4}-)?[0-9]{7,8}$&#x2F;.test(s)}</li>
<li>是否url地址<br>export const isURL &#x3D; (s) &#x3D;&gt; {    return &#x2F;^http[s]?://.*&#x2F;.test(s)}</li>
<li>是否字符串<br>export const isString &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘String’}</li>
<li>是否数字<br>export const isNumber &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Number’}</li>
<li>是否boolean<br>export const isBoolean &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Boolean’}</li>
<li>是否函数<br>export const isFunction &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Function’}</li>
<li>是否为null<br>export const isNull &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Null’}</li>
<li>是否undefined<br>export const isUndefined &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Undefined’}</li>
<li>是否对象<br>export const isObj &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Object’}</li>
<li>是否数组<br>export const isArray &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Array’}</li>
<li>是否时间<br>export const isDate &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Date’}</li>
<li>是否正则<br>export const isRegExp &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘RegExp’}</li>
<li>是否错误对象<br>export const isError &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Error’}</li>
<li>是否Symbol函数<br>export const isSymbol &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Symbol’}</li>
<li>是否Promise对象<br>export const isPromise &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Promise’}</li>
<li>是否Set对象<br>export const isSet &#x3D; (o) &#x3D;&gt; {    return Object.prototype.toString.call(o).slice(8, -1) &#x3D;&#x3D;&#x3D; ‘Set’}export const ua &#x3D; navigator.userAgent.toLowerCase();</li>
<li>是否是微信浏览器<br>export const isWeiXin &#x3D; () &#x3D;&gt; {    return ua.match(&#x2F;microMessenger&#x2F;i) &#x3D;&#x3D; ‘micromessenger’}</li>
<li>是否是移动端<br>export const isDeviceMobile &#x3D; () &#x3D;&gt; {    return &#x2F;android|webos|iphone|ipod|balckberry&#x2F;i.test(ua)}</li>
<li>是否是QQ浏览器<br>export const isQQBrowser &#x3D; () &#x3D;&gt; {    return !!ua.match(&#x2F;mqqbrowser|qzone|qqbrowser|qbwebviewtype&#x2F;i)}</li>
<li>是否是爬虫<br>export const isSpider &#x3D; () &#x3D;&gt; {    return &#x2F;adsbot|googlebot|bingbot|msnbot|yandexbot|baidubot|robot|careerbot|seznambot|bot|baiduspider|jikespider|symantecspider|scannerlwebcrawler|crawler|360spider|sosospider|sogou web sprider|sogou orion spider&#x2F;.test(ua)}</li>
<li>是否ios<br>export const isIos &#x3D; () &#x3D;&gt; {    var u &#x3D; navigator.userAgent;    if (u.indexOf(‘Android’) &gt; -1 || u.indexOf(‘Linux’) &gt; -1) {  &#x2F;&#x2F;安卓手机        return false    } else if (u.indexOf(‘iPhone’) &gt; -1) {&#x2F;&#x2F;苹果手机        return true    } else if (u.indexOf(‘iPad’) &gt; -1) {&#x2F;&#x2F;iPad        return false    } else if (u.indexOf(‘Windows Phone’) &gt; -1) {&#x2F;&#x2F;winphone手机        return false    } else {        return false    }}</li>
<li>是否为PC端<br>export const isPC &#x3D; () &#x3D;&gt; {    var userAgentInfo &#x3D; navigator.userAgent;    var Agents &#x3D; [“Android”, “iPhone”,        “SymbianOS”, “Windows Phone”,        “iPad”, “iPod”];    var flag &#x3D; true;    for (var v &#x3D; 0; v &lt; Agents.length; v++) {        if (userAgentInfo.indexOf(Agents[v]) &gt; 0) {            flag &#x3D; false;            break;        }    }    return flag;}</li>
<li>去除html标签<br>export const removehtmltag &#x3D; (str) &#x3D;&gt; {    return str.replace(&#x2F;&lt;[^&gt;]+&gt;&#x2F;g, ‘’)}</li>
<li>获取url参数<br>export const getQueryString &#x3D; (name) &#x3D;&gt; {    const reg &#x3D; new RegExp(‘(^|&amp;)’ + name + ‘&#x3D;([^&amp;]*)(&amp;|$)’, ‘i’);    const search &#x3D; window.location.search.split(‘?’)[1] || ‘’;    const r &#x3D; search.match(reg) || [];    return r[2];}</li>
<li>动态引入js<br>export const injectScript &#x3D; (src) &#x3D;&gt; {    const s &#x3D; document.createElement(‘script’);    s.type &#x3D; ‘text&#x2F;JavaScript’;    s.async &#x3D; true;    s.src &#x3D; src;    const t &#x3D; document.getElementsByTagName(‘script’)[0];    t.parentNode.insertBefore(s, t);}</li>
<li>根据url地址下载<br>export const download &#x3D; (url) &#x3D;&gt; {    var isChrome &#x3D; navigator.userAgent.toLowerCase().indexOf(‘chrome’) &gt; -1;    var isSafari &#x3D; navigator.userAgent.toLowerCase().indexOf(‘safari’) &gt; -1;    if (isChrome || isSafari) {        var link &#x3D; document.createElement(‘a’);        link.href &#x3D; url;        if (link.download !&#x3D;&#x3D; undefined) {            var fileName &#x3D; url.substring(url.lastIndexOf(‘&#x2F;‘) + 1, url.length);            link.download &#x3D; fileName;        }        if (document.createEvent) {            var e &#x3D; document.createEvent(‘MouseEvents’);            e.initEvent(‘click’, true, true);            link.dispatchEvent(e);            return true;        }    }    if (url.indexOf(‘?’) &#x3D;&#x3D;&#x3D; -1) {        url +&#x3D; ‘?download’;    }    window.open(url, ‘_self’);    return true;}</li>
<li>el是否包含某个class<br>export const hasClass &#x3D; (el, className) &#x3D;&gt; {    let reg &#x3D; new RegExp(‘(^|\s)’ + className + ‘(\s|$)’)    return reg.test(el.className)}</li>
<li>el添加某个class<br>export const addClass &#x3D; (el, className) &#x3D;&gt; {    if (hasClass(el, className)) {        return    }    let newClass &#x3D; el.className.split(‘ ‘)    newClass.push(className)    el.className &#x3D; newClass.join(‘ ‘)}</li>
<li>el去除某个class<br>export const removeClass &#x3D; (el, className) &#x3D;&gt; {    if (!hasClass(el, className)) {        return    }    let reg &#x3D; new RegExp(‘(^|\s)’ + className + ‘(\s|$)’, ‘g’)    el.className &#x3D; el.className.replace(reg, ‘ ‘)}</li>
<li>获取滚动的坐标<br>export const getScrollPosition &#x3D; (el &#x3D; window) &#x3D;&gt; ({    x: el.pageXOffset !&#x3D;&#x3D; undefined ? el.pageXOffset : el.scrollLeft,    y: el.pageYOffset !&#x3D;&#x3D; undefined ? el.pageYOffset : el.scrollTop});</li>
<li>滚动到顶部<br>export const scrollToTop &#x3D; () &#x3D;&gt; {    const c &#x3D; document.documentElement.scrollTop || document.body.scrollTop;    if (c &gt; 0) {        window.requestAnimationFrame(scrollToTop);        window.scrollTo(0, c - c &#x2F; 8);    }}</li>
<li>el是否在视口范围内<br>export const elementIsVisibleInViewport &#x3D; (el, partiallyVisible &#x3D; false) &#x3D;&gt; {    const { top, left, bottom, right } &#x3D; el.getBoundingClientRect();    const { innerHeight, innerWidth } &#x3D; window;    return partiallyVisible        ? ((top &gt; 0 &amp;&amp; top &lt; innerHeight) || (bottom &gt; 0 &amp;&amp; bottom &lt; innerHeight)) &amp;&amp;        ((left &gt; 0 &amp;&amp; left &lt; innerWidth) || (right &gt; 0 &amp;&amp; right &lt; innerWidth))        : top &gt;&#x3D; 0 &amp;&amp; left &gt;&#x3D; 0 &amp;&amp; bottom &lt;&#x3D; innerHeight &amp;&amp; right &lt;&#x3D; innerWidth;}</li>
<li>洗牌算法随机<br>export const shuffle &#x3D; (arr) &#x3D;&gt; {    var result &#x3D; [],        random;    while (arr.length &gt; 0) {        random &#x3D; Math.floor(Math.random() * arr.length);        result.push(arr[random])        arr.splice(random, 1)    }    return result;}</li>
<li>拦截粘贴板<br>export const copyTextToClipboard &#x3D; (value) &#x3D;&gt; {    var textArea &#x3D; document.createElement(“textarea”);    textArea.style.background &#x3D; ‘transparent’;    textArea.value &#x3D; value;    document.body.appendChild(textArea);    textArea.select();    try {        var successful &#x3D; document.execCommand(‘copy’);    } catch (err) {        console.log(‘Oops, unable to copy’);    }    document.body.removeChild(textArea);}</li>
<li>判断类型集合<br>export const checkStr &#x3D; (str, type) &#x3D;&gt; {    switch (type) {        case ‘phone’:   &#x2F;&#x2F;手机号码            return &#x2F;^1[3|4|5|6|7|8|9][0-9]{9}$&#x2F;.test(str);        case ‘tel’:     &#x2F;&#x2F;座机            return &#x2F;^(0\d{2,3}-\d{7,8})(-\d{1,4})?$&#x2F;.test(str);        case ‘card’:    &#x2F;&#x2F;身份证            return &#x2F;(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)&#x2F;.test(str);        case ‘pwd’:     &#x2F;&#x2F;密码以字母开头，长度在6<del>18之间，只能包含字母. 数字和下划线            return &#x2F;^[a-zA-Z]\w{5,17}$&#x2F;.test(str)        case ‘postal’:  &#x2F;&#x2F;邮政编码            return &#x2F;[1-9]\d{5}(?!\d)&#x2F;.test(str);        case ‘QQ’:      &#x2F;&#x2F;QQ号            return &#x2F;^[1-9][0-9]{4,9}$&#x2F;.test(str);        case ‘email’:   &#x2F;&#x2F;邮箱            return &#x2F;^[\w-]+(.[\w-]+)<em>@[\w-]+(.[\w-]+)+$&#x2F;.test(str);        case ‘money’:   &#x2F;&#x2F;金额(小数点2位)            return &#x2F;^\d</em>(?:.\d{0,2})?$&#x2F;.test(str);        case ‘URL’:     &#x2F;&#x2F;网址            return &#x2F;(http|ftp|https)://[\w-<em>]+(.[\w-</em>]+)+([\w-.,@?^&#x3D;%&amp;:&#x2F;</del>+#]<em>[\w-@?^&#x3D;%&amp;&#x2F;~+#])?&#x2F;.test(str)        case ‘IP’:      &#x2F;&#x2F;IP            return &#x2F;((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))&#x2F;.test(str);        case ‘date’:    &#x2F;&#x2F;日期时间            return &#x2F;^(\d{4})-(\d{2})-(\d{2}) (\d{2})(?::\d{2}|:(\d{2}):(\d{2}))$&#x2F;.test(str) || &#x2F;^(\d{4})-(\d{2})-(\d{2})$&#x2F;.test(str)        case ‘number’:  &#x2F;&#x2F;数字            return &#x2F;^[0-9]$&#x2F;.test(str);        case ‘english’: &#x2F;&#x2F;英文            return &#x2F;^[a-zA-Z]+$&#x2F;.test(str);        case ‘chinese’: &#x2F;&#x2F;中文            return &#x2F;^[\u4E00-\u9FA5]+$&#x2F;.test(str);        case ‘lower’:   &#x2F;&#x2F;小写            return &#x2F;^[a-z]+$&#x2F;.test(str);        case ‘upper’:   &#x2F;&#x2F;大写            return &#x2F;^[A-Z]+$&#x2F;.test(str);        case ‘HTML’:    &#x2F;&#x2F;HTML标记            return &#x2F;&lt;(“[^”]*”|’[^’]*’|[^’”&gt;])</em>&gt;&#x2F;.test(str);        default:            return true;    }}</li>
<li>严格的身份证校验<br>export const isCardID &#x3D; (sId) &#x3D;&gt; {    if (!&#x2F;(^\d{15}$)|(^\d{17}(\d|X|x)$)&#x2F;.test(sId)) {        console.log(‘你输入的身份证长度或格式错误’)        return false    }    &#x2F;&#x2F;身份证城市    var aCity &#x3D; { 11: “北京”, 12: “天津”, 13: “河北”, 14: “山西”, 15: “内蒙古”, 21: “辽宁”, 22: “吉林”, 23: “黑龙江”, 31: “上海”, 32: “江苏”, 33: “浙江”, 34: “安徽”, 35: “福建”, 36: “江西”, 37: “山东”, 41: “河南”, 42: “湖北”, 43: “湖南”, 44: “广东”, 45: “广西”, 46: “海南”, 50: “重庆”, 51: “四川”, 52: “贵州”, 53: “云南”, 54: “西藏”, 61: “陕西”, 62: “甘肃”, 63: “青海”, 64: “宁夏”, 65: “新疆”, 71: “台湾”, 81: “香港”, 82: “澳门”, 91: “国外” };    if (!aCity[parseInt(sId.substr(0, 2))]) {        console.log(‘你的身份证地区非法’)        return false    }<br>&#x2F;&#x2F; 出生日期验证    var sBirthday &#x3D; (sId.substr(6, 4) + “-“ + Number(sId.substr(10, 2)) + “-“ + Number(sId.substr(12, 2))).replace(&#x2F;-&#x2F;g, “&#x2F;“),        d &#x3D; new Date(sBirthday)    if (sBirthday !&#x3D; (d.getFullYear() + “&#x2F;“ + (d.getMonth() + 1) + “&#x2F;“ + d.getDate())) {        console.log(‘身份证上的出生日期非法’)        return false    }<br>&#x2F;&#x2F; 身份证号码校验    var sum &#x3D; 0,        weights &#x3D; [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],        codes &#x3D; “10X98765432”    for (var i &#x3D; 0; i &lt; sId.length - 1; i++) {        sum +&#x3D; sId[i] * weights[i];    }    var last &#x3D; codes[sum % 11]; &#x2F;&#x2F;计算出来的最后一位身份证号码    if (sId[sId.length - 1] !&#x3D; last) {        console.log(‘你输入的身份证号非法’)        return false    }<br>return true}</li>
<li>随机数范围<br>export const random &#x3D; (min, max) &#x3D;&gt; {    if (arguments.length &#x3D;&#x3D;&#x3D; 2) {        return Math.floor(min + Math.random() * ((max + 1) - min))    } else {        return null;    }}</li>
<li>将阿拉伯数字翻译成中文的大写数字<br>export const numberToChinese &#x3D; (num) &#x3D;&gt; {    var AA &#x3D; new Array(“零”, “一”, “二”, “三”, “四”, “五”, “六”, “七”, “八”, “九”, “十”);    var BB &#x3D; new Array(“”, “十”, “百”, “仟”, “萬”, “億”, “点”, “”);    var a &#x3D; (“” + num).replace(&#x2F;(^0*)&#x2F;g, “”).split(“.”),        k &#x3D; 0,        re &#x3D; “”;    for (var i &#x3D; a[0].length - 1; i &gt;&#x3D; 0; i–) {        switch (k) {            case 0:                re &#x3D; BB[7] + re;                break;            case 4:                if (!new RegExp(“0{4}&#x2F;&#x2F;d{“ + (a[0].length - i - 1) + “}$”)                    .test(a[0]))                    re &#x3D; BB[4] + re;                break;            case 8:                re &#x3D; BB[5] + re;                BB[7] &#x3D; BB[5];                k &#x3D; 0;                break;        }        if (k % 4 &#x3D;&#x3D; 2 &amp;&amp; a[0].charAt(i + 2) !&#x3D; 0 &amp;&amp; a[0].charAt(i + 1) &#x3D;&#x3D; 0)            re &#x3D; AA[0] + re;        if (a[0].charAt(i) !&#x3D; 0)            re &#x3D; AA[a[0].charAt(i)] + BB[k % 4] + re;        k++;    }<br>if (a.length &gt; 1) &#x2F;&#x2F; 加上小数部分(如果有小数部分)    {        re +&#x3D; BB[6];        for (var i &#x3D; 0; i &lt; a[1].length; i++)            re +&#x3D; AA[a[1].charAt(i)];    }    if (re &#x3D;&#x3D; ‘一十’)        re &#x3D; “十”;    if (re.match(&#x2F;^一&#x2F;) &amp;&amp; re.length &#x3D;&#x3D; 3)        re &#x3D; re.replace(“一”, “”);    return re;}</li>
<li>将数字转换为大写金额<br>export const changeToChinese &#x3D; (Num) &#x3D;&gt; {    &#x2F;&#x2F;判断如果传递进来的不是字符的话转换为字符    if (typeof Num &#x3D;&#x3D; “number”) {        Num &#x3D; new String(Num);    };    Num &#x3D; Num.replace(&#x2F;,&#x2F;g, “”) &#x2F;&#x2F;替换tomoney()中的“,”    Num &#x3D; Num.replace(&#x2F; &#x2F;g, “”) &#x2F;&#x2F;替换tomoney()中的空格    Num &#x3D; Num.replace(&#x2F;￥&#x2F;g, “”) &#x2F;&#x2F;替换掉可能出现的￥字符    if (isNaN(Num)) { &#x2F;&#x2F;验证输入的字符是否为数字        &#x2F;&#x2F;alert(“请检查小写金额是否正确”);        return “”;    };    &#x2F;&#x2F;字符处理完毕后开始转换，采用前后两部分分别转换    var part &#x3D; String(Num).split(“.”);    var newchar &#x3D; “”;    &#x2F;&#x2F;小数点前进行转化    for (var i &#x3D; part[0].length - 1; i &gt;&#x3D; 0; i–) {        if (part[0].length &gt; 10) {            return “”;            &#x2F;&#x2F;若数量超过拾亿单位，提示        }        var tmpnewchar &#x3D; “”        var perchar &#x3D; part[0].charAt(i);        switch (perchar) {            case “0”:                tmpnewchar &#x3D; “零” + tmpnewchar;                break;            case “1”:                tmpnewchar &#x3D; “壹” + tmpnewchar;                break;            case “2”:                tmpnewchar &#x3D; “贰” + tmpnewchar;                break;            case “3”:                tmpnewchar &#x3D; “叁” + tmpnewchar;                break;            case “4”:                tmpnewchar &#x3D; “肆” + tmpnewchar;                break;            case “5”:                tmpnewchar &#x3D; “伍” + tmpnewchar;                break;            case “6”:                tmpnewchar &#x3D; “陆” + tmpnewchar;                break;            case “7”:                tmpnewchar &#x3D; “柒” + tmpnewchar;                break;            case “8”:                tmpnewchar &#x3D; “捌” + tmpnewchar;                break;            case “9”:                tmpnewchar &#x3D; “玖” + tmpnewchar;                break;        }        switch (part[0].length - i - 1) {            case 0:                tmpnewchar &#x3D; tmpnewchar + “元”;                break;            case 1:                if (perchar !&#x3D; 0) tmpnewchar &#x3D; tmpnewchar + “拾”;                break;            case 2:                if (perchar !&#x3D; 0) tmpnewchar &#x3D; tmpnewchar + “佰”;                break;            case 3:                if (perchar !&#x3D; 0) tmpnewchar &#x3D; tmpnewchar + “仟”;                break;            case 4:                tmpnewchar &#x3D; tmpnewchar + “万”;                break;            case 5:                if (perchar !&#x3D; 0) tmpnewchar &#x3D; tmpnewchar + “拾”;                break;            case 6:                if (perchar !&#x3D; 0) tmpnewchar &#x3D; tmpnewchar + “佰”;                break;            case 7:                if (perchar !&#x3D; 0) tmpnewchar &#x3D; tmpnewchar + “仟”;                break;            case 8:                tmpnewchar &#x3D; tmpnewchar + “亿”;                break;            case 9:                tmpnewchar &#x3D; tmpnewchar + “拾”;                break;        }        var newchar &#x3D; tmpnewchar + newchar;    }    &#x2F;&#x2F;小数点之后进行转化    if (Num.indexOf(“.”) !&#x3D; -1) {        if (part[1].length &gt; 2) {            &#x2F;&#x2F; alert(“小数点之后只能保留两位,系统将自动截断”);            part[1] &#x3D; part[1].substr(0, 2)        }        for (i &#x3D; 0; i &lt; part[1].length; i++) {            tmpnewchar &#x3D; “”            perchar &#x3D; part[1].charAt(i)            switch (perchar) {                case “0”:                    tmpnewchar &#x3D; “零” + tmpnewchar;                    break;                case “1”:                    tmpnewchar &#x3D; “壹” + tmpnewchar;                    break;                case “2”:                    tmpnewchar &#x3D; “贰” + tmpnewchar;                    break;                case “3”:                    tmpnewchar &#x3D; “叁” + tmpnewchar;                    break;                case “4”:                    tmpnewchar &#x3D; “肆” + tmpnewchar;                    break;                case “5”:                    tmpnewchar &#x3D; “伍” + tmpnewchar;                    break;                case “6”:                    tmpnewchar &#x3D; “陆” + tmpnewchar;                    break;                case “7”:                    tmpnewchar &#x3D; “柒” + tmpnewchar;                    break;                case “8”:                    tmpnewchar &#x3D; “捌” + tmpnewchar;                    break;                case “9”:                    tmpnewchar &#x3D; “玖” + tmpnewchar;                    break;            }            if (i &#x3D;&#x3D; 0) tmpnewchar &#x3D; tmpnewchar + “角”;            if (i &#x3D;&#x3D; 1) tmpnewchar &#x3D; tmpnewchar + “分”;            newchar &#x3D; newchar + tmpnewchar;        }    }    &#x2F;&#x2F;替换所有无用汉字    while (newchar.search(“零零”) !&#x3D; -1)        newchar &#x3D; newchar.replace(“零零”, “零”);    newchar &#x3D; newchar.replace(“零亿”, “亿”);    newchar &#x3D; newchar.replace(“亿万”, “亿”);    newchar &#x3D; newchar.replace(“零万”, “万”);    newchar &#x3D; newchar.replace(“零元”, “元”);    newchar &#x3D; newchar.replace(“零角”, “”);    newchar &#x3D; newchar.replace(“零分”, “”);    if (newchar.charAt(newchar.length - 1) &#x3D;&#x3D; “元”) {        newchar &#x3D; newchar + “整”    }    return newchar;    }</li>
<li>判断一个元素是否在数组中<br>export const contains &#x3D; (arr, val) &#x3D;&gt; {    return arr.indexOf(val) !&#x3D; -1 ? true : false;}</li>
<li>数组排序，{type} 1：从小到大 2：从大到小 3：随机<br>export const sort &#x3D; (arr, type &#x3D; 1) &#x3D;&gt; {    return arr.sort((a, b) &#x3D;&gt; {        switch (type) {            case 1:                return a - b;            case 2:                return b - a;            case 3:                return Math.random() - 0.5;            default:                return arr;        }    })}</li>
<li>去重<br>export const unique &#x3D; (arr) &#x3D;&gt; {    if (Array.hasOwnProperty(‘from’)) {        return Array.from(new Set(arr));    } else {        var n &#x3D; {}, r &#x3D; [];        for (var i &#x3D; 0; i &lt; arr.length; i++) {            if (!n[arr[i]]) {                n[arr[i]] &#x3D; true;                r.push(arr[i]);            }        }        return r;    }}</li>
<li>求两个集合的并集<br>export const union &#x3D; (a, b) &#x3D;&gt; {    var newArr &#x3D; a.concat(b);    return this.unique(newArr);}</li>
<li>求两个集合的交集<br>export const intersect &#x3D; (a, b) &#x3D;&gt; {    var _this &#x3D; this;    a &#x3D; this.unique(a);    return this.map(a, function (o) {        return _this.contains(b, o) ? o : null;    });}</li>
<li>删除其中一个元素<br>export const remove &#x3D; (arr, ele) &#x3D;&gt; {    var index &#x3D; arr.indexOf(ele);    if (index &gt; -1) {        arr.splice(index, 1);    }    return arr;}</li>
<li>将类数组转换为数组<br>export const formArray &#x3D; (ary) &#x3D;&gt; {    var arr &#x3D; [];    if (Array.isArray(ary)) {        arr &#x3D; ary;    } else {        arr &#x3D; Array.prototype.slice.call(ary);    };    return arr;}</li>
<li>最大值<br>export const max &#x3D; (arr) &#x3D;&gt; {    return Math.max.apply(null, arr);}</li>
<li>最小值<br>export const min &#x3D; (arr) &#x3D;&gt; {    return Math.min.apply(null, arr);}</li>
<li>求和<br>export const sum &#x3D; (arr) &#x3D;&gt; {    return arr.reduce((pre, cur) &#x3D;&gt; {        return pre + cur    })}</li>
<li>平均值<br>export const average &#x3D; (arr) &#x3D;&gt; {    return this.sum(arr) &#x2F; arr.length}</li>
<li>去除空格,type: 1-所有空格 2-前后空格 3-前空格 4-后空格<br>export const trim &#x3D; (str, type) &#x3D;&gt; {    type &#x3D; type || 1    switch (type) {        case 1:            return str.replace(&#x2F;\s+&#x2F;g, “”);        case 2:            return str.replace(&#x2F;(^\s*)|(\s*$)&#x2F;g, “”);        case 3:            return str.replace(&#x2F;(^\s*)&#x2F;g, “”);        case 4:            return str.replace(&#x2F;(\s*$)&#x2F;g, “”);        default:            return str;    }}</li>
<li>字符转换<br>type: 1:首字母大写 2：首字母小写 3：大小写转换 4：全部大写 5：全部小写<br>export const changeCase &#x3D; (str, type) &#x3D;&gt; {    type &#x3D; type || 4    switch (type) {        case 1:            return str.replace(&#x2F;\b\w+\b&#x2F;g, function (word) {                return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();<br>});        case 2:            return str.replace(&#x2F;\b\w+\b&#x2F;g, function (word) {                return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();            });        case 3:            return str.split(‘’).map(function (word) {                if (&#x2F;[a-z]&#x2F;.test(word)) {                    return word.toUpperCase();                } else {                    return word.toLowerCase()                }            }).join(‘’)        case 4:            return str.toUpperCase();        case 5:            return str.toLowerCase();        default:            return str;    }}</li>
<li>检测密码强度<br>export const checkPwd &#x3D; (str) &#x3D;&gt; {    var Lv &#x3D; 0;    if (str.length &lt; 6) {        return Lv    }    if (&#x2F;[0-9]&#x2F;.test(str)) {        Lv++    }    if (&#x2F;[a-z]&#x2F;.test(str)) {        Lv++    }    if (&#x2F;[A-Z]&#x2F;.test(str)) {        Lv++    }    if (&#x2F;[.|-|_]&#x2F;.test(str)) {        Lv++    }    return Lv;}</li>
<li>函数节流器<br>export const debouncer &#x3D; (fn, time, interval &#x3D; 200) &#x3D;&gt; {    if (time - (window.debounceTimestamp || 0) &gt; interval) {        fn &amp;&amp; fn();        window.debounceTimestamp &#x3D; time;    }}</li>
<li>在字符串中插入新字符串<br>export const insertStr &#x3D; (soure, index, newStr) &#x3D;&gt; {    var str &#x3D; soure.slice(0, index) + newStr + soure.slice(index);    return str;}</li>
<li>判断两个对象是否键值相同<br>export const isObjectEqual &#x3D; (a, b) &#x3D;&gt; {    var aProps &#x3D; Object.getOwnPropertyNames(a);    var bProps &#x3D; Object.getOwnPropertyNames(b);<br>if (aProps.length !&#x3D;&#x3D; bProps.length) {        return false;    }<br>for (var i &#x3D; 0; i &lt; aProps.length; i++) {        var propName &#x3D; aProps[i];<br>if (a[propName] !&#x3D;&#x3D; b[propName]) {            return false;        }    }    return true;}</li>
<li>16进制颜色转RGBRGBA字符串<br>export const colorToRGB &#x3D; (val, opa) &#x3D;&gt; {<br>var pattern &#x3D; &#x2F;^(#?)[a-fA-F0-9]{6}$&#x2F;; &#x2F;&#x2F;16进制颜色值校验规则    var isOpa &#x3D; typeof opa &#x3D;&#x3D; ‘number’; &#x2F;&#x2F;判断是否有设置不透明度<br>if (!pattern.test(val)) { &#x2F;&#x2F;如果值不符合规则返回空字符        return ‘’;    }<br>var v &#x3D; val.replace(&#x2F;#&#x2F;, ‘’); &#x2F;&#x2F;如果有#号先去除#号    var rgbArr &#x3D; [];    var rgbStr &#x3D; ‘’;<br>for (var i &#x3D; 0; i &lt; 3; i++) {        var item &#x3D; v.substring(i * 2, i * 2 + 2);        var num &#x3D; parseInt(item, 16);        rgbArr.push(num);    }<br>rgbStr &#x3D; rgbArr.join();    rgbStr &#x3D; ‘rgb’ + (isOpa ? ‘a’ : ‘’) + ‘(‘ + rgbStr + (isOpa ? ‘,’ + opa : ‘’) + ‘)’;    return rgbStr;}</li>
<li>追加url参数<br>export const appendQuery &#x3D; (url, key, value) &#x3D;&gt; {    var options &#x3D; key;    if (typeof options &#x3D;&#x3D; ‘string’) {        options &#x3D; {};        options[key] &#x3D; value;    }    options &#x3D; $.param(options);    if (url.includes(‘?’)) {        url +&#x3D; ‘&amp;’ + options    } else {        url +&#x3D; ‘?’ + options    }    return url;}</li>
<li>从数组中获取最小值&#x2F;最大值<br>您可以使用 Math.min() 或 Math.max() 结合扩展运算符来查找数组中的最小值或最大值。<br>const numbers &#x3D; [6, 8, 1, 3, 9];<br>console.log(Math.max(…numbers)); &#x2F;&#x2F; 9console.log(Math.min(…numbers)); &#x2F;&#x2F; 1</li>
<li>复制到剪贴板<br>您可以使用 Clipboard API 创建“复制到剪贴板”功能：<br>function copyToClipboard(text) {    navigator.clipboard.writeText(text);}</li>
</ol>

        </div>

        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/tags/%E5%89%8D%E7%AB%AF/">#前端</a>&nbsp;
                    </li>
                
            </ul>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2022/03/12/SpringMvc%E6%97%A0web-xml%E9%85%8D%E7%BD%AE/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">SpringMvc无web.xml配置</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2022/03/09/git%E6%8E%A8%E9%80%81%E4%BB%A3%E7%A0%81%E4%BB%A3%E7%A0%81http413%E9%97%AE%E9%A2%98/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">git提交push到远程时出现error: RPC failed; HTTP 413 curl 22</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2020</span>
              -
            
            2022&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">Axuan</a>
        </div>
        
        <div class="theme-info info-item">
            由 <a target="_blank" href="https://hexo.io">Hexo</a> 驱动&nbsp;|&nbsp;主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>



    
<script src="/js/local-search.js"></script>




    
<script src="/js/code-copy.js"></script>




    
<script src="/js/lazyload.js"></script>



<div class="post-scripts">
    
        
<script src="/js/left-side-toggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/toc.js"></script>

    
</div>



</body>
</html>
